//
//  SXTNextView.m
//  8.18项目
//
//  Created by ma c on 16/8/18.
//  Copyright © 2016年 zhang. All rights reserved.
//

#import "SXTNextView.h"

@interface SXTNextView ()<UITextFieldDelegate>

@property (strong, nonatomic)   UILabel *tostLabel;              /** 提示验证码发送到哪个手机号中 */
@property (strong, nonatomic)   UITextField *verficationText;              /** 输入验证码的text */
@property (strong, nonatomic)   UIButton *showTime;              /** 显示剩余等待时间的button */
@property (strong, nonatomic)   UIButton *landingBtn;              /** 注册按钮 */
@property (strong, nonatomic)   UILabel *whiteBackLabe;              /** 白色背景 */
@property (strong, nonatomic)   UILabel *lineLabel;              /** 细分割线 */
@end


@implementation SXTNextView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self addSubview:self.tostLabel];
        [self addSubview:self.whiteBackLabe];
        [self addSubview:self.verficationText];
        [self addSubview:self.landingBtn];
        [self addSubview:self.showTime];
        [self addSubview:self.lineLabel];
    }
    return self;
}

//布局
-(void)layoutSubviews {

    [super layoutSubviews];
    __weak typeof(self) weakSelf = self;
    [_tostLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@35);
        make.left.equalTo(weakSelf.mas_left).offset(15);
        make.top.right.equalTo(weakSelf);
    }];
    [_whiteBackLabe mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.tostLabel.mas_bottom);
        make.height.equalTo(@43);
        make.left.equalTo(weakSelf.mas_left).offset(-1);
        make.right.equalTo(weakSelf.mas_right).offset(1);
    }];
    [_verficationText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.whiteBackLabe.mas_left).offset(15);
        make.top.bottom.equalTo(weakSelf.whiteBackLabe);
        make.right.equalTo(weakSelf.whiteBackLabe.mas_right).offset(VIEW_WIDTH-140);
    }];
    [_showTime mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(140, 44));
        make.right.top.equalTo(weakSelf.whiteBackLabe);
    }];
    [_landingBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@35);
        make.left.equalTo(weakSelf.mas_left).offset(15);
        make.right.equalTo(weakSelf.mas_right).offset(-15);
        make.top.equalTo(weakSelf.verficationText.mas_bottom).offset(16);
    }];
    [_lineLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(1, 30));
        make.centerY.equalTo(weakSelf.whiteBackLabe.mas_centerY);
        make.right.equalTo(weakSelf.showTime.mas_left).offset(-1);
    }];
}

//拿到手机号
-(void)setPhoneNumText:(NSString *)phoneNumText {

    _phoneNumText = phoneNumText;
    _tostLabel.attributedText = [self makeTostAttributed];
}

#pragma mark - 初始化控件

- (UILabel *)tostLabel{

    if (!_tostLabel) {
        _tostLabel = [[UILabel alloc]init];
        _tostLabel.font = [UIFont systemFontOfSize:12.0];
    }
    return _tostLabel;
}
//验证码
- (UITextField *)verficationText {

    if (!_verficationText) {
        _verficationText = [[UITextField alloc]init];
        _verficationText.delegate = self;
        _verficationText.placeholder = @"请输入验证码";
        _verficationText.keyboardType = UIKeyboardTypeNumberPad;
        [_verficationText addTarget:self action:@selector(verficationFieldChangeText:) forControlEvents:(UIControlEventEditingChanged)];
    }
    return _verficationText;
}

-(UILabel *)whiteBackLabe {

    if (!_whiteBackLabe) {
        _whiteBackLabe = [[UILabel alloc]init];
        _whiteBackLabe.backgroundColor = [UIColor whiteColor];
        _whiteBackLabe.layer.borderColor = RGB(188, 186,192).CGColor;
        _whiteBackLabe.layer.borderWidth = 1.0;
    }
    return _whiteBackLabe;
}

-(UIButton *)showTime{
    
    if (!_showTime) {
        _showTime = [UIButton buttonWithType:(UIButtonTypeCustom)];
        _showTime.titleLabel.textAlignment = NSTextAlignmentCenter;
        [_showTime setTitle:@"获取验证码" forState:(UIControlStateNormal)];
        [_showTime addTarget:self action:@selector(GCDTime) forControlEvents:(UIControlEventTouchUpInside)];
        [_showTime setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    }
    return _showTime;
}

-(UIButton *)landingBtn {

    if (!_landingBtn) {
        _landingBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_landingBtn setTitle:@"注   册" forState:(UIControlStateNormal)];
        [_landingBtn setTitleColor:RGB(132, 132, 132) forState:(UIControlStateNormal)];
        [_landingBtn setTitleColor:RGB(229, 229, 229) forState:(UIControlStateSelected)];
        _landingBtn.selected = NO;
        _landingBtn.backgroundColor = RGB(229, 229, 229);
        _landingBtn.layer.masksToBounds = YES;
        _landingBtn.layer.cornerRadius = 5.0;
        [_landingBtn addTarget:self action:@selector(landingBtnMethod) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _landingBtn;
}

-(UILabel *)lineLabel{

    if (!_lineLabel) {
        _lineLabel = [[UILabel alloc]init];
        _lineLabel.backgroundColor = RGB(188, 186, 192);
    }
    return _lineLabel;
}


//开始获取验证码
- (void)startTimeRequest {
    if (_showTimeBlock) {
        _showTimeBlock();
    }

}
//注册按钮点击调用
-(void)landingBtnMethod {
    if (_landingBtnBlock) {
        _landingBtnBlock(_verficationText.text);
    }
}

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{

    if (textField == _verficationText && range.location == 6) {
        
        return NO;
    }
    return YES;
}

- (void)verficationFieldChangeText:(UITextField *) textFiled {

    if (textFiled.text.length == 6) {
        _landingBtn.backgroundColor = RGB(56, 166, 241);
        _landingBtn.userInteractionEnabled = YES;
        _landingBtn.selected = YES;
    }else{
    
        _landingBtn.backgroundColor = RGB(229, 229, 229);
        _landingBtn.userInteractionEnabled = NO;
        _landingBtn.selected = NO;
    }
}
//倒计时
-(void)GCDTime {

 __block NSInteger time = 60;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 1.0*NSEC_PER_SEC, 0*NSEC_PER_SEC);
    dispatch_source_set_event_handler(timer, ^{
        
        if (time < 1) {
            dispatch_source_cancel(timer);
            NSMutableAttributedString * string = [[NSMutableAttributedString alloc]initWithString:@"重新发送" attributes:@{NSForegroundColorAttributeName:RGB(56, 166, 243)}];
            dispatch_async(dispatch_get_main_queue(), ^{
                _showTime.userInteractionEnabled = YES;
               
                [_showTime setAttributedTitle:string forState:(UIControlStateNormal)];
            });
            
        }else{
        
        dispatch_async(dispatch_get_main_queue(), ^{
            _showTime.userInteractionEnabled = NO;
            [_showTime setAttributedTitle:[self makeShowBtnText:time] forState:(UIControlStateNormal)];
        });
            time --;
        }
    });
    dispatch_resume(timer);
}

- (NSMutableAttributedString *)makeTostAttributed {
    NSMutableAttributedString * string1 = [[NSMutableAttributedString alloc]initWithString:@"验证码已经发送到" attributes:@{NSForegroundColorAttributeName:RGB(139, 139, 139)}];
    NSMutableAttributedString * string2 = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"+86 %@",_phoneNumText] attributes:@{NSForegroundColorAttributeName:RGB(56, 166, 243)}];
    [string1 insertAttributedString:string2 atIndex:string1.length];
    return string1;
}

//showTime显示的内容方法
- (NSMutableAttributedString *)makeShowBtnText:(NSInteger)time {

    NSDictionary *attributedDic1 = @{NSForegroundColorAttributeName:RGB(56, 166, 241)};
    NSString * string = [NSString stringWithFormat:@"%li ",time];
    NSAttributedString * attributedString1 = [[NSAttributedString alloc]initWithString:string attributes:attributedDic1];
    
    NSDictionary * attributedDic2 = @{NSForegroundColorAttributeName:RGB(133, 133, 133)};
    NSAttributedString * attributedString2 = [[NSAttributedString alloc]initWithString:@" 秒后重试" attributes:attributedDic2];
    
    NSMutableAttributedString * mutableAttributed = [[NSMutableAttributedString alloc]init];
    [mutableAttributed insertAttributedString:attributedString1 atIndex:0];
    [mutableAttributed insertAttributedString:attributedString2 atIndex:attributedString1.length];
    return mutableAttributed;
}







@end
